
<html>
<body>
<?php
session_start();
/* Please supply your own consumer key and consumer secret */
error_reporting  (E_ALL);
ini_set ('display_errors', true);
$consumerKey = 'slnt58rtvm6gkvx';
$consumerSecret = 'ksqbvu5fv3tq02t';

include 'Dropbox/autoload.php';
//echo $consumerKey;
//echo $consumerSecret;

//$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);

// If the PHP OAuth extension is not available, you can try
// PEAR's HTTP_OAUTH instead.
$oauth = new Dropbox_OAuth_PEAR($consumerKey,$consumerSecret);

//if(isset($_SESSION['oauth_tokens']))
//{
//	$oauth->setToken($_SESSION['tokens']);
//	echo "TRUE";
//	die();
//}

$dropbox = new Dropbox_API($oauth);

// We need to start a session
//session_start();
//echo "<hr>Session Started<hr>";

// There are multiple steps in this workflow, we keep a 'state number' here
if (isset($_SESSION['state'])) {
    $state = $_SESSION['state'];
	if(isset($_POST['state']))
	{ 
		$state = $_POST['state'];
	}
}
else {
    $state = 0;
}

print_r($_SESSION);
	
echo $state;

switch($state) {

    /* In this phase we grab the initial request tokens
       and redirect the user to the 'authorize' page hosted
       on dropbox */
	
	case 0 : echo "Authenicate with Dropbox";
			 echo "<form action='dropbox.php' method='get'> <input type='submit' value='Authenticte'</form>";
			 $_SESSION['state'] = 1;
			 break;
    case 1 :
        //echo "Step 1: Acquire request tokens\n";
        $tokens = $oauth->getRequestToken();
        //print_r($tokens);

        // Note that if you want the user to automatically redirect back, you can
        // add the 'callback' argument to getAuthorizeUrl.
        //echo "Step 2: You must now redirect the user to:\n";
        $url = $oauth->getAuthorizeUrl("http://thegodzilladrive.com/pennapps/bootstrap/index.php");
        //echo $url;
        $_SESSION['state'] = 2;
        $_SESSION['oauth_tokens1'] = $tokens;
        header("Location: " . $url);
        break;

    /* In this phase, the user just came back from authorizing
       and we're going to fetch the real access tokens */
    case 2 :
        //echo "Step 3: Acquiring access tokens\n";
        $oauth->setToken($_SESSION['oauth_tokens1']);
        $tokens = $oauth->getAccessToken() or die("WHAT?");
        //print_r($tokens);
        $_SESSION['state'] = 3;
        $_SESSION['oauth_tokens'] = $tokens;
        // There is no break here, intentional

    /* This part gets called if the authentication process
       already succeeded. We can use our stored tokens and the api 
       should work. Store these tokens somewhere, like a database */
    case 3 :
        //echo "The user is authenticated\n";
        //echo "You should really save the oauth tokens somewhere, so the first steps will no longer be needed\n";
        //print_r($_SESSION['oauth_tokens']);
        $oauth->setToken($_SESSION['oauth_tokens']);
        $_SESSION['state'] = 4;
       
        header("Location: http://thegodzilladrive.com/pennapps/bootstrap/index.php");
        break;
    
    case 4: 
    	 if(isset($_SESSION['oauth_tokens']))
    	 {
    	 	echo $_SESSION['oauth_tokens'];
    	 	$oauth->setToken($_SESSION['oauth_tokens']);
       	
    	 }
    	 echo "Upload File";
		 echo '<form action="dropbox.php" method="post"
				enctype="multipart/form-data" name="form1">
				<label for="file">Filename:</label>
				<input type="file" name="file" id="file"><br>
				<input type="hidden" name="state" value="5">
				<input type="submit" name="Upload" value="Submit">
			   </form>';
	
		  echo '
		  		<form action="dropbox.php" method="post" name="form2"
				enctype="multipart/form-data">
				<input type="hidden" name="state" value="6">
				<input type="submit" name="GetMetadata" value="GetMetadata">
			   </form>';
		  
		 echo '
		  		<form action="dropbox.php" method="post" name="form3"
				enctype="multipart/form-data">
				<input type="hidden" name="state" value="7">
				<input type="submit" name="directory_list" value="directory listing">
			   </form>';
		  
		  
		 
		 break;
    case 5 :
	 	
		if ($_FILES["file"]["error"] > 0)
		  {
		  echo "Error: " . $_FILES["file"]["error"] . "<br>";
		  }
		else
		  {
		  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
		  echo "Type: " . $_FILES["file"]["type"] . "<br>";
		  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
		 /// echo "Stored in: " . $_FILES["file"]["tmp_name"];
		  }
		  $h = $_FILES["file"]["tmp_name"];
		 // echo $h ."\n";
		  if(isset($_SESSION['oauth_tokens']))
    	  {
    	 	echo $_SESSION['oauth_tokens'];
    	 	$oauth->setToken($_SESSION['oauth_tokens']);
       		$dropbox = new Dropbox_API($oauth,'sandbox');
			$dropbox->putFile('newPath.txt',$h);
		  	echo "Uplodaded";
		  } 
		  break;
    case 6:
    	
    	 if(isset($_SESSION['oauth_tokens1']))
    	 {
    	 	//get real tokens	
    	 	  	$oauth->setToken($_SESSION['oauth_tokens1']);
		        $tokens = $oauth->getAccessToken() or die("WHAT?");
		        //print_r($tokens);
		        $_SESSION['state'] = 3;
		        $_SESSION['oauth_tokens'] = $tokens;
      
    	 }
    	
    	 if(isset($_SESSION['oauth_tokens']))
    	  {
    	 	echo $_SESSION['oauth_tokens'];
    	 	$oauth->setToken($_SESSION['oauth_tokens']);
       		$dropbox = new Dropbox_API($oauth,'sandbox');
		  	print_r($dropbox->getAccountInfo());
    	  } 
			break;
      case 7: 
		if(isset($_SESSION['oauth_tokens1']))
		    	 {
		    	 	//get real tokens	
		    	 	  	$oauth->setToken($_SESSION['oauth_tokens1']);
				        $tokens = $oauth->getAccessToken() or die("WHAT?");
				        //print_r($tokens);
				        $_SESSION['state'] = 3;
				        $_SESSION['oauth_tokens'] = $tokens;
		      
		    	 }
		if(isset($_SESSION['oauth_tokens']))
    	  {
    	 	echo $_SESSION['oauth_tokens'];
    	 	$oauth->setToken($_SESSION['oauth_tokens']);
       		$dropbox = new Dropbox_API($oauth,'sandbox');
			$_SESSION['dropbox'] = $dropbox->getMetadata('/',true);
			$_SESSION['state'] = -1;
			//print_r($_SESSION['dropbox']['contents']);
			//print_r($dropbox->getMetadata('/',false));
			header("Location: http://thegodzilladrive.com/pennapps/bootstrap/index.php");
		  } 
}
    


?>
</body>
</html>